package com.atguigu.gmall.search.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.search.dao.GoodsRepository;
import com.atguigu.gmall.search.entity.Goods;
import com.atguigu.gmall.search.service.GoodsSearchService;
import com.atguigu.gmall.search.vo.SearchParamVo;
import com.atguigu.gmall.search.vo.SearchRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lfy
 * @Description
 * @create 2023-07-24 9:13
 */
@RequestMapping("/api/inner/search")
@RestController
public class SearchRpcController {

    @Autowired
    GoodsRepository goodsRepository;

    @Autowired
    GoodsSearchService searchService;


    /**
     * 修改商品的热度分
     * @param skuId
     * @param score
     * @return
     */
    @GetMapping("/hotscore/{skuId}/{score}")
    public Result incrHotScore(@PathVariable("skuId") Long skuId,
                               @PathVariable("score") Long score){

        searchService.updateSkuHotScore(skuId,score);
        return Result.ok();
    }

    /**
     * 检索
     * @param searchParamVo
     * @return
     */
    @PostMapping("/search")
    public Result<SearchRespVo> search(@RequestBody SearchParamVo searchParamVo, HttpServletRequest request){

        //商品检索
        SearchRespVo respVo =  searchService.search(searchParamVo);
        return Result.ok(respVo);
    }


    /**
     * 上架
     * @param goods
     * @return
     */
    @PostMapping("/up")
    public Result up(@RequestBody Goods goods){

        goodsRepository.save(goods);
        return Result.ok();
    }

    /**
     * 下架
     * @param skuId
     * @return
     */
    @GetMapping("/down/{skuId}")
    public Result down(@PathVariable("skuId") Long skuId){

        goodsRepository.deleteById(skuId);
        return Result.ok();
    }
}
